﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shop_Phone.COMM;
using System.Data;
using System.Globalization;

namespace Shop_Phone.Admin.UserControls
{
    public partial class giamgia_add : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && Visible)
            {
                ucNotificationDialog.Hide();
                fill_danhmuc();
                fill_dm_sanpham(ddldanhmuc.SelectedValue);

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtgiamgia.Text))
            {
                NotificationMessage("giá giảm giá không được để trống", 0);
                return;
            }

            try
            {
                Admins vcontroller = new Admins();
                Entities.giamgia vgiamgia = new Entities.giamgia();
                string errMsg = string.Empty;
                vgiamgia.Masanpham = ddlsanpham.SelectedValue;
                vgiamgia.Giamoi = Convert.ToDecimal(txtgiamgia.Text);
                if (string.IsNullOrEmpty(txtngaybatdau.Text))
                {
                    vgiamgia.Ngaybatdau = DateTime.Now;
                }
                else
                {
                    vgiamgia.Ngaybatdau = DateTime.Parse(txtngaybatdau.Text);
                }
                vgiamgia.Ngayketthuc = Convert.ToDateTime(txtngayketthuc.Text, System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);
                vgiamgia.Trangthai = false;
                int result = vcontroller.save_giamgia(vgiamgia, ref errMsg);
                if (result > 0)
                {
                    ucNotificationDialog.MessageContent = "Thêm mới giảm thành công!";
                    ucNotificationDialog.MessageType = 1;
                    ucNotificationDialog.RedirectUrl = "~/Admin/giamgias.aspx";
                    ucNotificationDialog.Show();
                }
                else
                {
                    NotificationMessage("Vui lòng kiểm tra lại.", 0);
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                NotificationMessage("Hệ thống gặp sự cố trong quá trình thêm giảm giá mới!", 0);
            }
        }

        private void NotificationMessage(string _MessageContent, int _MessageType)
        {
            ucNotificationDialog.MessageContent = _MessageContent;
            ucNotificationDialog.MessageType = _MessageType;
            ucNotificationDialog.Show();
            return;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/giamgias.aspx");
        }
        protected void Calendar_SelectionChanged(System.Object sender, System.EventArgs e)
        {
            txtngaybatdau.Text = Convert.ToDateTime(Calendar.SelectedDate, CultureInfo.GetCultureInfo("en-US")).ToString("MM/dd/yyyy");
        }
        protected void Calendar2_SelectionChanged(System.Object sender, System.EventArgs e)
        {
            txtngayketthuc.Text = Convert.ToDateTime(Calendar1.SelectedDate, CultureInfo.GetCultureInfo("en-US")).ToString("MM/dd/yyyy");
        }
        private void fill_danhmuc()
        {
            Admins vcontroller = new Admins();
            ddldanhmuc.Items.Clear();
            string errMsg = string.Empty;
            try
            {
                DataTable dt = vcontroller.getall_danhmucsanpham(ref errMsg);

                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    { ddldanhmuc.Items.Add(new ListItem(Convert.ToString(dt.Rows[i]["tendanhmuc"]), Convert.ToString(dt.Rows[i]["madanhmuc"]))); }
                }
            }
            catch (Exception ex) { Response.Write(ex.Message); }
            finally { vcontroller = null; }
        }

        private void fill_dm_sanpham(string _madanhmuc)
        {
            Admins vcontroller = new Admins();
            string errMsg = string.Empty;
            ddlsanpham.Items.Clear();
            try
            {
                DataTable dt = vcontroller.getall_sanpham_dm(_madanhmuc, ref errMsg);

                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    { ddlsanpham.Items.Add(new ListItem(Convert.ToString(dt.Rows[i]["tensanpham"]), Convert.ToString(dt.Rows[i]["masanpham"]))); }
                }
            }
            catch (Exception ex) { Response.Write(ex.Message); }
            finally { vcontroller = null; }
        }

        protected void ddldanhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            string madanhmuc = ddldanhmuc.SelectedValue;
            fill_dm_sanpham(madanhmuc);
        }

        protected void ddlsanpham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Admins vcontroller = new Admins();
            Entities.sanpham vsanpham = new Entities.sanpham();
            string errMsg = string.Empty;
            vsanpham = vcontroller.getby_sanpham(ddlsanpham.SelectedValue.ToString(), ref  errMsg);
            txtgiacu.Text = vsanpham.Giasanpham.ToString();
        }
    }
}